﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;

namespace TP1___Gestion_Clavier
{
	public partial class MainPage : UserControl
	{
		public List<Bureau> liste_mobile;
		public List<Obstacle> liste_fixe;
		public List<prof> liste_ennemis;
		
		public MainPage()
		{
			// Requis pour initialiser des variables
			InitializeComponent();
			// Création des obstacles fixes
			liste_fixe = new List<Obstacle>();
			liste_fixe.Add(new Obstacle(150,0));
			liste_fixe.Add(new Obstacle(500,0));
			liste_fixe.Add(new Obstacle(350,50));
			liste_fixe.Add(new Obstacle(0,100));
			liste_fixe.Add(new Obstacle(200,150));
			liste_fixe.Add(new Obstacle(450,150));
			liste_fixe.Add(new Obstacle(100,300));
			liste_fixe.Add(new Obstacle(300,300));
			liste_fixe.Add(new Obstacle(500,300));
			liste_fixe.Add(new Obstacle(350,450));
			liste_fixe.Add(new Obstacle(0,500));
			liste_fixe.Add(new Obstacle(200,500));
			liste_fixe.Add(new Obstacle(500,500));
			
			liste_ennemis = new List<prof>();
			// Ajout des éléments mobiles
			liste_mobile = new List<Bureau>();
			int nb_bureaux = 0;
			Random rand = new Random();
			while (nb_bureaux < 5)
			{
				int x = rand.Next(11) * 50;
				int y = rand.Next(11) * 50;
				if (this.Zone_OK(x, y, 50, 50,Eleve.Direction.haut))
				{
					Bureau bur = new Bureau();
					bur.X = x;
					bur.Y = y;
					this.LayoutRoot.Children.Add(bur);
					liste_mobile.Add(bur);
					nb_bureaux++;
					bur.Draw();
				}
			}
			
			//Ajout des ennemis
			int nb_ennemis = 0;
			while (nb_ennemis < 3)
			{
				int x = rand.Next(11) * 50;
				int y = rand.Next(11) * 50;
				if (this.Zone_OK(x, y, 50, 50,Eleve.Direction.haut))
				{
					prof screen = new prof();
					screen.X = x;
					screen.Y = y;
					this.LayoutRoot.Children.Add(screen);
					liste_ennemis.Add(screen);
					screen.Draw();
					nb_ennemis++;
					
				}
			}
		}
		
		private void UserControl_Loaded(object sender, System.Windows.RoutedEventArgs e)
		{
			CompositionTarget.Rendering += new EventHandler(CompositionTarget_Rendering);
			EtatClavier.Ecouter(this);
			x_Eleve.cnvPere = this.LayoutRoot;
		}

		private void UserControl_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
		{
			x_Eleve.Y-=50;
			x_Eleve.Draw();
		}

		private void x_Eleve_MouseWheel(object sender, System.Windows.Input.MouseWheelEventArgs e)
		{
			if (e.Delta >0){
				x_Eleve.Y-=50;
			}else{
			x_Eleve.Y+=50;
			}
			x_Eleve.Draw();
		}
		
		public void CompositionTarget_Rendering(object sender, EventArgs e)
        {
            // Boucle de jeu
			x_Eleve.MiseAJour(this);
			System.Threading.Thread.Sleep(50);
			foreach (Bureau bur in liste_mobile){
				bur.Draw();
			}
			foreach (prof screen in liste_ennemis){
				screen.Draw();
			}
			//x_Eleve.Draw();
        }
		
		public bool Zone_OK(int _x, int _y, int _width, int _height, Eleve.Direction dir )
		{
			// Test murs extérieurs
			if (_x < 0 || _x > 500)
			{
				return false;
			}
			// ici : tests pour Y
			if (_y < 0 || _y > 500)
			{
				return false;
			}
			// Test obstacles
			foreach (Obstacle pilier in liste_fixe)
			{
				if ((pilier.X == _x) && (pilier.Y == _y))
				{
					return false;
				}
			}
			//Test écrans
			foreach (prof ecran in liste_ennemis)
			{
				if ((ecran.X == _x) && (ecran.Y == _y))
				{
					return false;
				}
			}
			//Deplacement bureaux
			foreach (Bureau bur in liste_mobile)
			{
				if ((bur.X == _x) && (bur.Y == _y))
				{
					return bur.Deplacer(dir,this);
				}
			}
			return true;
		}
	}
}